/*
 * Created by hql on #CreateTime#
 */

using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

namespace IMMotors
{
    public class FruitBehaviour : MonoBehaviour
    {
        public RectTransform rect;
        public RawImage bg;
        public RawImage fruit;
        public Animator animator;

        private void Awake()
        {
            rect = GetComponent<RectTransform>();
            animator = GetComponent<Animator>();
            bg = GetComponent<RawImage>();
            fruit = transform.GetChild(0).GetComponent<RawImage>();
            // transform.DOLocalRotate(new Vector3(0, 0, 360), 1, RotateMode.LocalAxisAdd);
        }

        public void Init(Vector3 startPos, Texture texture)
        {
            // rect.localPosition = startPos;
            rect.transform.position = startPos;
            rect.DOScale(Config.Instance.circleScale, 1f);
            bg.texture = Config.Instance.circles[Random.Range(0, Config.Instance.circles.Count)];
            fruit.texture = texture;
            Config.Instance.childs.Add(rect);
            GetComponent<CanvasGroup>().DOFade(1, 1f);
        }

        public void Out()
        {
            GetComponent<CanvasGroup>().DOFade(0, 1f);
            rect.DOScale(0, 1).OnComplete(() =>
            {
                Destroy(gameObject);
                Config.Instance.childs.Remove(rect);
            });
        }

        public void MoveToPoint(Vector3 pos)
        {
            // rect.DOLocalMove(pos, 3f).SetDelay(0.5f).OnComplete(() => { animator.SetBool("Rotate", true); });
            rect.transform.DOMove(pos, 3f).SetDelay(0.5f).OnComplete(() => { animator.SetBool("Rotate", true); });
        }

        public void PlayAnimation()
        {
            animator.SetBool("Rotate", true);
        }
    }
}
